// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   UserDetailsServiceImpl.java

package com.nervytech.tutorcruise.spring.security.auth;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.nervytech.tutorcruise.db.dao.model.User;
import com.nervytech.tutorcruise.db.enums.UserStatusEnum;
import com.nervytech.tutorcruise.db.enums.UserTypeEnum;
import com.nervytech.tutorcruise.db.service.interfaces.IUserService;

// Referenced classes of package com.nervytech.tutorcruise.spring.security.auth:
//            SessionUser

@Service("userDetailsService")
@SessionAttributes("user")
public class UserDetailsServiceImpl implements UserDetailsService {

	private static final Logger logger = LoggerFactory
			.getLogger(UserDetailsServiceImpl.class);

	@Autowired
	private IUserService userService;

	public UserDetails loadUserByUsername(String userEmail)
			throws UsernameNotFoundException, DataAccessException {
		SessionUser springUser = null;
		try {
			User user = userService.getUserByEmail(userEmail);
			if (user == null) {
				logger.info((new StringBuilder("User ")).append(userEmail)
						.append(" not found in the system.").toString());
				throw new UsernameNotFoundException("Oops!!! Invalid username.");
			}
			Collection authorities = new ArrayList();
			authorities.add(new SimpleGrantedAuthority(UserTypeEnum.forValue(
					user.getUserType()).name()));
			boolean enabled = false;
			if (user.getUserStatus() == UserStatusEnum.ENABLED.getStatus())
				enabled = true;
			springUser = new SessionUser(userEmail, user.getPassword(),
					authorities, enabled, user.getFullName(), user.getUserId());
			user.setLastLoginTime(new Date());
			userService.updateLastLoginTime(user.getUserId(),
					user.getLastLoginTime());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Error while checking if user exist : ", e);
			throw new UsernameNotFoundException(
					"Error while checking if user exist !!!");
		}
		return springUser;
	}

	public static SessionUser currentUserDetails() {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		Authentication authentication = securityContext.getAuthentication();
		if (authentication != null) {
			Object principal = authentication.getPrincipal();
			return (principal instanceof UserDetails) ? (SessionUser) principal
					: null;
		} else {
			return null;
		}
	}

}
